﻿# system
import cgi

# appengine
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

# local
import base

class SearchPage(webapp.RequestHandler):
  def get(self):
    q = self.request.get("q")
    if q:
      query = base.Person.all()
      query.filter('name =', q)
      persons = query.order('-last_update')
      template_values = base.GetPersonsTemplate(self, persons)
      template_values['q'] = cgi.escape(q)
    else:
      template_values = {}

    path = None
    if self.request.get("json"):
      path = base.GetTemplatePath("search_json.html")
      cb = self.request.get("callback")
      template_values['callback'] = cb
    else:
      path = base.GetTemplatePath("search.html")
    self.response.out.write(template.render(path, template_values))
